//
//  UpcomingItem.m
//  ViperDemo
//
//  Created by Leeesven on 17/5/20.
//  Copyright © 2017年 Leeesven. All rights reserved.
//

#import "UpcomingItem.h"

#import "NSCalendar+CalendarAdditions.h"

@interface UpcomingItem ()

@property (nonatomic, assign) VTDNearTermDateRelation dateRelation;
@property (nonatomic, strong) NSDate*                 dueDate;
@property (nonatomic, copy)   NSString*               title;

@end

@implementation UpcomingItem

+ (instancetype)upcomingItemWithDateRelation:(VTDNearTermDateRelation)dateRelation dueDate:(NSDate *)dueDate title:(NSString *)title
{
    UpcomingItem *item = [[UpcomingItem alloc] init];
    
    item.dateRelation = dateRelation;
    item.dueDate = dueDate;
    item.title = title;
    
    return item;
}


- (NSString *)description
{
    return [NSString stringWithFormat:@"%@    %ld  %@  %@", [super description], (long)self.dateRelation, [NSDateFormatter localizedStringFromDate:self.dueDate dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle], self.title];
}

- (BOOL)isEqualToUpcomingItem:(UpcomingItem *)item
{
    if (!item)
    {
        return NO;
    }
    
    BOOL hasEqualRelations = (self.dateRelation == item.dateRelation);
    BOOL hasEqualDueDates = (!self.dueDate && !item.dueDate) || [[NSCalendar autoupdatingCurrentCalendar] isDate:self.dueDate equalToYearMonthDay:item.dueDate];
    BOOL hasEqualTitles = (!self.title && !item.title) || [self.title isEqualToString:item.title];
    
    return hasEqualRelations && hasEqualDueDates && hasEqualTitles;
}


- (BOOL)isEqual:(id)object
{
    if (self == object)
    {
        return YES;
    }
    
    if (![object isKindOfClass:[UpcomingItem class]])
    {
        return NO;
    }
    
    return [self isEqualToUpcomingItem:object];
}


- (NSUInteger)hash
{
    return self.dateRelation ^ [self.dueDate hash] ^ [self.title hash];
}


@end
